$.ajax({
    "url": "../php/gouwuche.php",
    async:false,
    "data": `user=${u1}`,
    "dataType": "JSON",
    "success": data => {
        //创建拼接字符串
        var str = ``
        var b
        data.forEach(el => {
            if (sessionStorage.name == el.user) {
                if(el.xzk=='1'){
                    b="checked='checked'"
                }else if(el.xzk=='0'){
                    b=''
                }
                str += `
                    <ul id="${el.id}" class="list">
                        <li>
                            <input type="checkbox" name="xuanzk" ${b}>
                        </li>
                        <li>
                            <img class="shopImg" src="${el.img}" alt="">
                        </li>
                        <li>
                            <p class="jj">${el.name}</p>
                        </li>
                        <li>
                            <p>颜色：彩色</p>
                            <p class="sz">尺码：<span>${el.size}</span></p>
                        </li>
                        <li class="jg"><span>${el.price}</span>元</li>
                        <li>
                            <span class="num-btn">-</span>
                            <input type="text" class="num-inpt" value="${el.number}" stock_num="5">
                            <span class="num-btn">+</span>
                        </li>
                        <li class="jg"><span>${el.price * el.number}</span>元</li>
                        <li>
                            <h2>删除</h2>
                            <h2>移入收藏夹</h2>
                        </li>
                    </ul>
                `
            }
            $(".content").html(str);
        });
    }
})

//获取操作对象
var qx = document.getElementsByName('quanxuan')[0]
var xuans = document.getElementsByName('xuanzk')
var content = document.querySelector(".content")
var footer = document.querySelector('.footer')

$("#gouwuche").click(function (e) {
    //对象兼容
    var e = e || window.event
    //目标兼容
    var target = e.target || e.srcElement

    var f = 1;
    //判断当前点击的是否为全选
    if (target.name == 'quanxuan') {
        //遍历所有的选择框对象
        for (var i = 0; i < xuans.length; i++) {
            //判断当前全选框是否被选中
            if (target.checked) {
                xuans[i].checked = true
                f=1
                console.log(111);
                $.ajax({
                    "url": "../php/qxk.php",
                    "data": `&xzk=${f}&user=${u1}`,
                    "dataType": "JSON",
                    "success": data => {
    
                    }
                })
            } else {
                xuans[i].checked = false
                f=0
                console.log(222);
                $.ajax({
                    "url": "../php/qxk.php",
                    "data": `&xzk=${f}&user=${u1}`,
                    "dataType": "JSON",
                    "success": data => {
    
                    }
                })
            }
        }
        xzk()
        zongji()
    }
    //判断当前点击的是否为选择框
    if (target.name == 'xuanzk') {
        if ($(e.target).attr("checked") == "checked") {
            f = 0;
            var id1 = $(e.target).parent().parent().attr("id");
            $.ajax({
                "url": "../php/xzk.php",
                "data": `&xzk=${f}&id=${id1}&user=${u1}`,
                "dataType": "JSON",
                "success": data => {

                }
            })
        } else {
            f = 1;
            var id1 = $(e.target).parent().parent().attr("id");
            $.ajax({
                "url": "../php/xzk.php",
                "data": `&xzk=${f}&id=${id1}&user=${u1}`,
                "dataType": "JSON",
                "success": data => {

                }
            })
        }
        xzk()
        zongji()
    }
    //判断当前点击的是否为+按钮
    if (target.innerHTML == "+") {
        //获取他的上一个兄弟节点的数量
        var n = target.previousElementSibling.value
        n++
        //重新给他赋值
        target.previousElementSibling.value = n
        //获取当前元素的ul对象
        var ul = target.parentNode.parentNode
        //获取单价价格
        var price = ul.children[4].lastElementChild.innerHTML
        //计算金额
        var jine = n * price
        //重新给金额赋值
        ul.children[6].lastElementChild.innerHTML = jine
        zongji()

        var id1 = $(e.target).parent().parent().attr("id");
        var s1 = $(e.target).parent().prev().prev().children().next().children().text();
        s1 = s1.trim()
        var name1 = $(e.target).parent().prev().prev().prev().children().html();
        name1 = name1.trim()

        $.ajax({
            "url": "../php/jia.php",
            "data": `&number=${n}&user=${u1}&id=${id1}&size=${s1}&name=${name1}`,
            "dataType": "JSON",
            "success": data => {

            }
        })
    }
    //判断当前点击的是否-按钮
    if (target.innerHTML == "-") {
        //获取他的下一个兄弟节点的数量
        var n = target.nextElementSibling.value
        if (n <= 1) {
            n = 1
        } else {
            n--
        }
        //重新给他赋值
        target.nextElementSibling.value = n
        //获取当前元素的ul对象
        var ul = target.parentNode.parentNode
        //获取单价价格
        var price = ul.children[4].lastElementChild.innerHTML
        //计算金额
        var jine = n * price
        //重新给金额赋值
        ul.children[6].lastElementChild.innerHTML = jine
        zongji()

        var id1 = $(e.target).parent().parent().attr("id");
        var s1 = $(e.target).parent().prev().prev().children().next().children().text();
        s1 = s1.trim()
        var name1 = $(e.target).parent().prev().prev().prev().children().html();
        name1 = name1.trim()

        $.ajax({
            "url": "../php/jia.php",
            "data": `&number=${n}&user=${u1}&id=${id1}&size=${s1}&name=${name1}`,
            "dataType": "JSON",
            "success": data => {

            }
        })
    }
    //判断是否点击的是删除按钮
    if (target.innerHTML == "删除") {
        //给一个确认框
        if (confirm("亲，确认要删除该商品吗")) {
            target.parentNode.parentNode.remove()
            var id1 = $(e.target).parent().parent().attr("id");
            $.ajax({
                "url": "../php/delete.php",
                "data": `&id=${id1}&user=${u1}`,
                "dataType": "JSON",
                "success": data => {

                }
            })
        }
        xzk()
        zongji()
    }
    xzk()
})
//封装选择框功能函数
function xzk() {
    var a = 0 //选中框被选中框的次数
    //遍历所有的选中框对象
    for (var i = 0; i < xuans.length; i++) {
        //判断当前选中框是否被选中
        if (xuans[i].checked) {
            a++
        }
    }
    //判断当前被选中的次数是否等于选中框的长度
    if (a == xuans.length && xuans.length != 0) {
        qx.checked = true;
    } else {
        qx.checked = false;
    }
    var lis = footer.getElementsByTagName('li')
    lis[0].lastElementChild.innerHTML = a
}
//封装总计功能函数
function zongji() {
    var sum = 0;
    //获取所有商品
    var uls = content.getElementsByTagName('ul')
    //遍历每个商品对象
    for (var i = 0; i < uls.length; i++) {
        //获取当前商品中所有的子元素
        var lis = uls[i].children
        //判断当前商品是否被选中
        if (lis[0].firstElementChild.checked) {
            //获取当前商品的金额
            var jine = uls[i].children[6].lastElementChild.innerHTML
            //累加
            sum += parseFloat(jine)
        }
    }
    //把结果赋值给总计
    var lis = footer.getElementsByTagName('li')
    lis[1].lastElementChild.innerHTML = sum.toFixed(2)
}

if($(".list").length=='0'){
    $(".go").css("display", "block")
}else{
    $(".go").css("display", "none")
}

zongji()
var u1 = $(".dlu").html();
xzk()